package com.sl.core.engine.datasource;

import cn.hutool.core.util.ObjectUtil;
import lombok.SneakyThrows;
import org.anyline.data.param.ConfigStore;
import org.anyline.data.transaction.TransactionState;
import org.anyline.metadata.Table;
import org.anyline.service.AnylineService;
import org.anyline.service.init.DefaultService;

public class AnylineServiceProxy extends DefaultService {


    private AnylineService service;

    private TransactionState transactionState;


    public AnylineServiceProxy(AnylineService service) {
        this.service = service;
    }


    @SneakyThrows
    public void startTransaction() {
        if (ObjectUtil.isNotNull(transactionState)) {
            return;
        }
        TransactionState status = service.start();
        this.transactionState = status;
    }



    @Override
    public long insert(Table dest, Object data, String... columns) {
        return service.insert(dest, data, columns);
    }
    @Override
    public long update(Table dest, Object data, ConfigStore configs, String... columns) {
        return service.update(dest, data,configs, columns);
    }
    @SneakyThrows
    public void commit() {
        if (ObjectUtil.isNull(transactionState)) {
            return;
        }
        service.commit(transactionState);
        transactionState = null;
    }

    @SneakyThrows
    public void rollBack() {
        if (ObjectUtil.isNull(transactionState)) {
            return;
        }
        service.rollback(transactionState);
    }
}
